<?php
$config = new RM_Config;

$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);
$dateTimeFormat = $priceSystem->getDateformat();
list($dateFormat, $timeFormat) = explode(' ', $dateTimeFormat);
$phpDateFormat = $priceSystem->getDateformat(true);

$startDay = $config->getValue('rm_config_calendar_startday');
$disabledPeriods = $this->calendardata;

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

$qtyCriteria  = 1; // the number of units selected (there will always be 1)
$qtyAvailable = 1; // the number of units available (used for the dropdown list)

if (isset($_GET['isGroup']) && (int)$_GET['qty']>1 ){
    $qtyAvailable = (int)$_GET['qty'];
}

if (isset($criteria->quantity) && $criteria->quantity>1){
    $qtyCriteria = $criteria->quantity;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};
RM.Common = {};

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/common.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/lists.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'getconstants'); ?>"></script>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>

<!-- ExtJS Base -->
<?php
$systemConfig = Zend_Registry::get('config');
if ((int)$systemConfig->get('js')->get('debug')->get('user') == 1) {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
<?php
} else {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
<?php
}
?>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/ux/Ext.ux.form.DateTime.js"></script>

<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user_overrides.css">
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/HourlyPrices/css/datepicker.css" />

<div class="RM_datepicker_modal_container">
    <div class="RM_datepicker_modal_header">
            <?php echo $this->_translate->_('User.DatePicker','DateSelection');?>
    </div>
    <div class="RM_dateselector">
    <table width="100%" border="0">
        <tr>
            <td>
                <?php echo $this->_translate->_('StartDate');?>
            </td>
            <td>
                <div id="RM_start_dp_calendar"></div>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo $this->_translate->_('EndDate');?>
            </td>
            <td>
                <div id="RM_end_dp_calendar"></div>
            </td>
        </tr>
    </table>
    <!-- quantity selection -->
    <div id="RM_quantity_selection" style="display: none;">
    <table width="100%" border="0">
        <tr>
            <td>
                <div class="RM_quanitity_selection_label"><?php echo $this->_translate->_('QuantityToReserve');?></div>
            </td>
            <td>
                <div id="RM_quanitity_selection">
                <select id="RM_dateselector_qty" name="RM_dateselector_qty">
                <?php
                if (isset($_GET['isGroup']) && $qtyAvailable>1 ){
                    for ($i = 1; $i <= $qtyAvailable; $i++) {
                        if ($qtyCriteria == $i){
                            ?><option selected value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                        } else {
                            ?><option value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                        }
                    }
                }?>
                </select>
                </div>
            </td>
        </tr>
    </table>
    </div>
    <table width="100%" border="0">
        <tr>
            <td>
                <div id="RM_dateselector_message" style="padding-top: 20px;">
                    <?php echo $this->_translate->_('User.DatePicker','HelpMsg');?>
                </div>
            </td>
        </tr>
    </table>
    <div id="RM_dateselector_button">
        <input name="" type="button" value="OK" class="button" onclick="RM_dp_Submit()" />
    </div>
</div>

<script type="text/javascript">
var RM_dp_submitFormat = '<?php echo $phpDateFormat; ?>';

function RM_dp_Submit(){

    document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','SavingDates');?>";
    document.getElementById('RM_dateselector_button').style.display = "none";

    var startDate = Ext.getCmp('rm_date_startdate_time').getValue();
    var endDate = Ext.getCmp('rm_date_enddate_time').getValue();
    var quantity = document.getElementById('RM_dateselector_qty').value;

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reservations',
            action: 'setdatecriteriajson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat),
            enddate: endDate.format(RM_dp_submitFormat),
            qty: quantity
        },
        method: 'POST',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                // if the config is set to go to cart on selection
                if ("1" === "<?php echo $config->getValue('rm_config_gotocartsfterselection_enabled'); ?>"){
                    // cart url
                    var cartURL = RM.Common.AssembleURL({
                        controller : 'Reservations',
                        action: 'cart'
                    });
                    window.parent.location = cartURL;
                } else {
                    // reload the parent window
                    window.parent.location.reload();
                }
            } else {
                document.getElementById('RM_dateselector_message').innerHTML = json.error;
                document.getElementById('RM_dateselector_button').style.display = "block";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

Ext.onReady(function(){

    var disabledData = Ext.util.JSON.decode('<?php echo $disabledPeriods;?>');

    <?php
    if (is_object($criteria) && $criteria->start_datetime) {
        ?>
        var startDate = new Date(<?php echo strtotime($criteria->start_datetime); ?>000);
        <?php
    } else {
        ?>
        var startDate = new Date();
        startDate.setHours(0);
        startDate.setMinutes(0);
        <?php
    }
    if (is_object($criteria) && $criteria->end_datetime) {
        ?>
        var endDate = new Date(<?php echo strtotime($criteria->end_datetime); ?>000);
        <?php
    } else {
        ?>
        var endDate = new Date();
        endDate.setHours(23);
        endDate.setMinutes(0);
        <?php
    }
    ?>

    var startDatePicker = new Ext.ux.form.DateTime({
        id: "rm_date_startdate_time",
        renderTo: 'RM_start_dp_calendar',
        width: 250,
        dateFormat: "<?php echo $dateFormat; ?>",
        timeFormat: "<?php echo $timeFormat; ?>",
        monthNames: RM.Translate.Common.JSON.MonthNames,
        dayNames: RM.Translate.Common.JSON.DayLetters,
        dateValidator: function(value) {
            Ext.getCmp('rm_date_enddate_time').setValue(this.getValue());
            return true;
        },
        timeConfig: {
            increment: 15
        }
    });
    var endDatePicker = new Ext.ux.form.DateTime({
        id: "rm_date_enddate_time",
        renderTo: 'RM_end_dp_calendar',
        timeConfig: {
            increment: 15
        },
        width: 250,
        dateFormat: "<?php echo $dateFormat; ?>",
        timeFormat: "<?php echo $timeFormat; ?>",
        monthNames: RM.Translate.Common.JSON.MonthNames,
        dayNames: RM.Translate.Common.JSON.DayLetters,
    });

    startDatePicker.setValue(startDate);
    endDatePicker.setValue(endDate);

    // if it's a group show the qty feild
    if ("<?php if (isset($_GET['isGroup'])){
            echo $_GET['isGroup'];
        } else {
            echo "0";
        }?>" === "1"){
        if (parseInt("<?php echo $qtyAvailable;?>",10) >1){
            document.getElementById("RM_quantity_selection").style.display = "block";
        }
    }
});
</script>
        </body>
</html>